package com.kehutong.repair.utils;

import java.time.format.DateTimeFormatter;

import org.coraframework.inject.Inject;
import org.coraframework.inject.Singleton;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.orm.Pool;
import org.coraframework.util.Objects;

import com.kehutong.common.DoveClient;
import com.kehutong.repair.entity.Repairs;
import com.kehutong.repair.entity.RepairsType;
import com.kehutong.repair.enums.OperationType;

@Singleton
public class SendMessageUtils {


    static final Logger logger = LoggerFactory.getLogger(SendMessageUtils.class);

    @Inject
    private DoveClient doveClient;

    /**
     *  发送公众号消息模板
     *
     * @param companyNo
     * @param repairs
     * @param state
     * @throws Exception
     */
    public void sendWeChatMessageTemplate(String companyNo, Repairs repairs, int state) throws Exception {
        doveClient.post("/webChat-service/template/webChat/message/send", (http) -> {
            http.addParam("companyNo", companyNo);
            http.addParam("repairsId", repairs.getId());
            http.addParam("webChatId", repairs.getCreateById());
            http.addParam("templateId", " ");   //todo 微信公众号消息模板id
            http.addParam("data", getData(repairs,state).toString());
        });
    }

    /**
     *  发送企业微信消息模板 EnterpriseWeChat
     *
     * @param repairs
     * @param title
     * @param userIds
     * @throws Exception
     */
    public void sendEnterpriseWeChatMessageTemplate(Repairs repairs, String title, String userIds, String companyNo) throws Exception {
        doveClient.post("/wwin-service/template/wwin/message/send", http -> {
            http.addHeader("companyNo", companyNo);
            JSONObject JSONParam = new JSONObject();
            JSONParam.put("id", repairs.getId());
            JSONParam.put("userIds", userIds);
            JSONParam.put("title", title);
            JSONParam.put("content", getEnterpriseWeChatMessageTemplateContent(repairs));
            http.setBody(JSONParam.toString());
        });
    }

    /**
     *  企业微信消息模板的内容
     *
     * @param repairs
     * @return
     */
    private String getEnterpriseWeChatMessageTemplateContent(Repairs repairs) {
        final StringBuilder content = new StringBuilder()
                .append("<div class=\"normal\">").append("业务类型：").append(repairs.getTypeName()).append("</div>")
                .append("<div class=\"normal\">").append("工单号：").append(repairs.getNoId()).append("</div>")
                .append("<div class=\"normal\">").append("创建时间：").append(repairs.getCreateTime().toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).append("</div>")
                .append("<div class=\"normal\">").append("小区名称：").append(repairs.getHouseInfo()).append("</div>");


        if(Objects.equal(repairs.getType(), OperationType.REPAIRS.getId())){
            content.append("<div class=\"normal\">").append("故障地点：").append(repairs.getFaultPosition()).append("</div>");
        }else {
            content.append("<div class=\"normal\">").append("投诉地点：").append(repairs.getHouseInfo()).append("</div>");
        }

        return content.toString();
    }

    /**
     * 封装body信息
     * @param repairs
     * @return
     */
    private JSONObject getData(Repairs repairs,int state){
        JSONObject jsonData = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("value",repairs.getNoId());
        jsonObject.put("color","#173177");
        jsonData.put("keyword1",jsonObject);

        RepairsType repairsType = Pool.get(RepairsType.class, repairs.getTypeId());
        jsonObject = new JSONObject();
        jsonObject.put("value",repairsType.getType().getDesc() + "-" + repairsType.getName());
        jsonObject.put("color","#173177");
        jsonData.put("keyword2",jsonObject);

        jsonObject = new JSONObject();
        if(state == 1){
            jsonObject.put("value","尊敬的业主您好！您的投诉/保修单正在处理请您耐心等候");
        }else if(state == 0){
            jsonObject.put("value","尊敬的业主您好！您的投诉/保修单添加了一条新的备注");
        } else{
            jsonObject.put("value","尊敬的业主您好！您的投诉/保修单已经处理完成");
        }
        jsonObject.put("color","#173177");
        jsonData.put("keyword3",jsonObject);

        jsonObject = new JSONObject();
        //jsonObject.put("value",repairs.getAssignTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        jsonObject.put("color","#173177");
        jsonData.put("keyword4",jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("value","点击查看工单详情");
        jsonObject.put("color","#173177");
        jsonData.put("remark",jsonObject);

        return jsonData;
    }
}
